using System;
using System.ComponentModel;
using System.Drawing;
using System.Net;
using System.Windows.Forms;

namespace ICP_MS.Plugin.iMass;

public class FormLIMS : Form
{
	private IContainer components = null;

	private TabControl tabControl1;

	private TabPage tabPage1;

	private TextBox txtPort;

	private Label label2;

	private TextBox txtIp;

	private Label label1;

	private GroupBox groupBox2;

	private RadioButton radioButtonTCP;

	private RadioButton radioButtonUDP;

	private Button btnCancel;

	private Button btnOk;

	private Button button1;

	public FormLIMS()
	{
		InitializeComponent();
	}

	private void FormConfig_Load(object sender, EventArgs e)
	{
		Text = Lang.Txt("AddiMass.Text9");
		groupBox2.Text = Lang.Txt("AddiMass.Text10");
		label1.Text = Lang.Txt("AddiMass.Text11");
		label2.Text = Lang.Txt("AddiMass.Text12");
		button1.Text = Lang.Txt("AddiMass.Text13");
		btnOk.Text = Lang.Txt("AddiMass.Text14");
		btnCancel.Text = Lang.Txt("AddiMass.Text15");
		txtIp.Text = ClientLIMS.Instance.HostIP;
		txtPort.Text = ClientLIMS.Instance.Port.ToString();
		radioButtonTCP.Checked = ClientLIMS.Instance.Type == 0;
		radioButtonUDP.Checked = ClientLIMS.Instance.Type == 1;
		UserManager.UserChanged += UserManager_UserChanged;
		UserManager_UserChanged(null, null);
		ClientLIMS.Instance.Init();
	}

	private void ExportLimsTypeChanged(object sender, EventArgs e)
	{
		if (radioButtonTCP.Checked)
		{
			ClientLIMS.Instance.Type = 0;
		}
		if (radioButtonUDP.Checked)
		{
			ClientLIMS.Instance.Type = 1;
		}
	}

	private void btnOk_Click(object sender, EventArgs e)
	{
		try
		{
			IPAddress ip = IPAddress.Parse(txtIp.Text);
			ClientLIMS.Instance.HostIP = txtIp.Text;
		}
		catch
		{
			MessageBox.Show("ip地址格式错误!");
			return;
		}
		try
		{
			int port = Convert.ToInt32(txtPort.Text);
			if (port <= 0 || port > 65535)
			{
				throw new Exception();
			}
			ClientLIMS.Instance.Port = port;
		}
		catch
		{
			MessageBox.Show("tcp/udp端口错误!");
			return;
		}
		ClientLIMS.Instance.CreateXML();
		base.DialogResult = DialogResult.OK;
	}

	private void btnCancel_Click(object sender, EventArgs e)
	{
		base.DialogResult = DialogResult.Cancel;
	}

	private void button1_Click(object sender, EventArgs e)
	{
		ClientLIMS.Instance.Init();
		ClientLIMS.Instance.SetErrorMessage();
	}

	private void FormLIMS_FormClosing(object sender, FormClosingEventArgs e)
	{
		UserManager.UserChanged -= UserManager_UserChanged;
	}

	private void UserManager_UserChanged(object sender, EventArgs e)
	{
		if (UserManager.CurrentUser.authority <= 1)
		{
			SetEnabled(enable: false);
		}
		else
		{
			SetEnabled(enable: true);
		}
	}

	private void SetEnabled(bool enable)
	{
		groupBox2.Enabled = enable;
		button1.Enabled = enable;
		btnOk.Enabled = enable;
		btnCancel.Enabled = enable;
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.tabControl1 = new System.Windows.Forms.TabControl();
		this.tabPage1 = new System.Windows.Forms.TabPage();
		this.txtPort = new System.Windows.Forms.TextBox();
		this.label2 = new System.Windows.Forms.Label();
		this.txtIp = new System.Windows.Forms.TextBox();
		this.label1 = new System.Windows.Forms.Label();
		this.groupBox2 = new System.Windows.Forms.GroupBox();
		this.radioButtonTCP = new System.Windows.Forms.RadioButton();
		this.radioButtonUDP = new System.Windows.Forms.RadioButton();
		this.btnCancel = new System.Windows.Forms.Button();
		this.btnOk = new System.Windows.Forms.Button();
		this.button1 = new System.Windows.Forms.Button();
		this.tabControl1.SuspendLayout();
		this.tabPage1.SuspendLayout();
		this.groupBox2.SuspendLayout();
		base.SuspendLayout();
		this.tabControl1.Controls.Add(this.tabPage1);
		this.tabControl1.Location = new System.Drawing.Point(78, 17);
		this.tabControl1.Name = "tabControl1";
		this.tabControl1.SelectedIndex = 0;
		this.tabControl1.Size = new System.Drawing.Size(266, 191);
		this.tabControl1.TabIndex = 2;
		this.tabPage1.BackColor = System.Drawing.SystemColors.Control;
		this.tabPage1.Controls.Add(this.txtPort);
		this.tabPage1.Controls.Add(this.label2);
		this.tabPage1.Controls.Add(this.txtIp);
		this.tabPage1.Controls.Add(this.label1);
		this.tabPage1.Location = new System.Drawing.Point(4, 22);
		this.tabPage1.Name = "tabPage1";
		this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
		this.tabPage1.Size = new System.Drawing.Size(258, 165);
		this.tabPage1.TabIndex = 0;
		this.tabPage1.Text = "Tcp";
		this.txtPort.Location = new System.Drawing.Point(51, 100);
		this.txtPort.Name = "txtPort";
		this.txtPort.Size = new System.Drawing.Size(171, 21);
		this.txtPort.TabIndex = 3;
		this.txtPort.Text = "8001";
		this.label2.AutoSize = true;
		this.label2.Location = new System.Drawing.Point(27, 85);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(35, 12);
		this.label2.TabIndex = 2;
		this.label2.Text = "端口:";
		this.txtIp.Location = new System.Drawing.Point(51, 46);
		this.txtIp.Name = "txtIp";
		this.txtIp.Size = new System.Drawing.Size(171, 21);
		this.txtIp.TabIndex = 1;
		this.txtIp.Text = "192.168.1.100";
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(15, 31);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(47, 12);
		this.label1.TabIndex = 0;
		this.label1.Text = "IP地址:";
		this.groupBox2.Controls.Add(this.radioButtonTCP);
		this.groupBox2.Controls.Add(this.radioButtonUDP);
		this.groupBox2.Controls.Add(this.tabControl1);
		this.groupBox2.Location = new System.Drawing.Point(12, 2);
		this.groupBox2.Name = "groupBox2";
		this.groupBox2.Size = new System.Drawing.Size(350, 220);
		this.groupBox2.TabIndex = 15;
		this.groupBox2.TabStop = false;
		this.groupBox2.Text = "选择传输方式";
		this.radioButtonTCP.AutoSize = true;
		this.radioButtonTCP.Checked = true;
		this.radioButtonTCP.Location = new System.Drawing.Point(8, 39);
		this.radioButtonTCP.Name = "radioButtonTCP";
		this.radioButtonTCP.Size = new System.Drawing.Size(41, 16);
		this.radioButtonTCP.TabIndex = 18;
		this.radioButtonTCP.TabStop = true;
		this.radioButtonTCP.Text = "TCP";
		this.radioButtonTCP.UseVisualStyleBackColor = true;
		this.radioButtonTCP.CheckedChanged += new System.EventHandler(ExportLimsTypeChanged);
		this.radioButtonUDP.AutoSize = true;
		this.radioButtonUDP.Location = new System.Drawing.Point(8, 90);
		this.radioButtonUDP.Name = "radioButtonUDP";
		this.radioButtonUDP.Size = new System.Drawing.Size(41, 16);
		this.radioButtonUDP.TabIndex = 16;
		this.radioButtonUDP.Text = "UDP";
		this.radioButtonUDP.UseVisualStyleBackColor = true;
		this.radioButtonUDP.CheckedChanged += new System.EventHandler(ExportLimsTypeChanged);
		this.btnCancel.Location = new System.Drawing.Point(264, 234);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 17;
		this.btnCancel.Text = "取消";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.btnOk.Location = new System.Drawing.Point(157, 234);
		this.btnOk.Name = "btnOk";
		this.btnOk.Size = new System.Drawing.Size(75, 23);
		this.btnOk.TabIndex = 16;
		this.btnOk.Text = "确定";
		this.btnOk.UseVisualStyleBackColor = true;
		this.btnOk.Click += new System.EventHandler(btnOk_Click);
		this.button1.Location = new System.Drawing.Point(61, 234);
		this.button1.Name = "button1";
		this.button1.Size = new System.Drawing.Size(75, 23);
		this.button1.TabIndex = 18;
		this.button1.Text = "测试";
		this.button1.UseVisualStyleBackColor = true;
		this.button1.Click += new System.EventHandler(button1_Click);
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(372, 269);
		base.Controls.Add(this.button1);
		base.Controls.Add(this.btnCancel);
		base.Controls.Add(this.btnOk);
		base.Controls.Add(this.groupBox2);
		base.Name = "FormLIMS";
		this.Text = "配置";
		base.FormClosing += new System.Windows.Forms.FormClosingEventHandler(FormLIMS_FormClosing);
		base.Load += new System.EventHandler(FormConfig_Load);
		this.tabControl1.ResumeLayout(false);
		this.tabPage1.ResumeLayout(false);
		this.tabPage1.PerformLayout();
		this.groupBox2.ResumeLayout(false);
		this.groupBox2.PerformLayout();
		base.ResumeLayout(false);
	}
}
